package assists
{
	public class ArrayUtils
	{
		/**
		 *根据数组里的某个属性 寻找数组里该项
		 */		
		public static  function seekByIndex(list:Array,index:String):Object
		{
			for(var i:int=0;i<list.length;i++)
			{
				if(list[i].index == index)
				{
					return list[i]
				}
			}
			return {};
		}
		/**
		 * 向数组里添加信息
		 */		
		public static function insertToArray(list:Array,index:*,pos:int):void
		{
		    list.splice(pos,0,index);	
		}
		/**
		 * 链接数组
		 */		
		public static function contackArrays(list1:Array,list2:Array):Array
		{
			list2.reverse();
			list2.shift();
			return list1.concat(list2);
		}
		/**
		 * 向数组里添加 数组 并加添加数组里的数据每一项加进去
		 */		
		public static function instertListToArray(list:Array,insertedList:Array,startPos:int):void
		{
			for(var i:int=list.length - 1;i>= 0 ; -- i)
			{
				insertToArray(insertedList,list[i],startPos);
			}
		}
		/**
		 * 在数组里寻找从startIndex 到 endIndex 之间的数据
		 */		
		public static function seekRange(list:Array,startIndex:String,endIndex:String,proverty:String = null):Array
		{
	        var startPos:int;
			var endPos:int;
			var len:int = list.length;
			for(var i:int=0;i<len;i++)
			{
				if(proverty != null)
				{
					if(list[i][proverty] == startIndex)
					{
						startPos = i;
					}else if(list[i][proverty] == endIndex)
					{
						endPos = i;
					}
				}else
				{
					if(list[i] == startIndex)
					{
						startPos = i;
					}else if(list[i] == endIndex)
					{
						endPos = i;
					}
				}
			}
			
			return list;
		}
		/**
		 * 比较长数组是否包含小数组
		 */		
		public static function matchList(shortList:Array,longList:Array,property:String):Boolean
		{
			var start:Boolean;
			for(var i:int=0;i<shortList.length - 1;i++)
			{
				for(var j:int=0;j<longList.length - 1;j++)
				{
					if(shortList[i][property] == longList[j][property])
					{ 
						start = true;
					    break;
					}
				}
				if(start) return true;
			}
			return false;
		}
		
		public static function removeListByIndex(removeList:Array,startIndex:String,endIndex:String,property:String = null):Array
		{
			var startPos:int;
			var endPos:int;
			var cicleList:Array=[];
			var isCircle:Boolean;
			for(var i:int=0;i<removeList.length;i++)
			{
				if(property != null)
				{
					if(removeList[i][property] == startIndex)
					{
						if(removeList[i +　1][property] == startIndex)//删掉环线中重复的结点
						{
							isCircle = true;
							removeList.splice(i,1);//删掉重复的环头结点
							-- i;
			cicleList = removeCircle(removeList,i + 1);
			startPos = i + 1;
						}else
						{
						startPos = i;
						}
					}else if(removeList[i][property] == endIndex)
					{
						if(removeList[i +　1][property] == endIndex)
						{
							removeList.splice(i,1);//删掉重复的环头结点
							isCircle = true;
						}
						endPos = i;
						break;
					}
				}
			}
			if(!isCircle)
			{
				cicleList = removeList.splice(startPos + cicleList.length,endPos - startPos + 1 - cicleList.length);
			}else
			{
				
			cicleList = cicleList.concat(removeList.splice(startPos + cicleList.length,endPos - startPos + 1 - cicleList.length));
			cicleList.shift();
			cicleList.pop();
			}
			return cicleList;
		}
		
		private static function removeCircle(list:Array,pos:int):Array
		{
			var off:int = 1;
			while(list[pos + off] == list[pos - off])
			{
				++ off;
			}
			off = off - 2
				if(off < 0) off = 0;
		return	list.splice(pos + 1,off);
			
		}
		
		public static function checkInList(obj:*,list:Array):Boolean
		{
			for(var i:int=0;i<list.length;i++)
			{
				if(list[i] == obj)
				{
					return true;
				}
			}
			return false;
		}
		/**
		 * 打日志
		 */		
		public static function printArray(list:Array,index:String):void
		{
			for(var i:int=0;i<list.length;i++)
			{
				trace("打印数组日志：" + list[i][index]);
			}
		}
		
	}
}